# -*- coding: utf-8 -*-
# @Time : 2022/6/8 下午3:10
# @Author : wyf
# @FileName: 作业6-1.py
# @Email: yunfei.wang@innvote.com
import requests
from lxml import etree
from pymongo import MongoClient

"""
## 第一种
mongodb = pymongo.MongoClient(f'mongodb://{username}:{password}@localhost:27017/{databasename}', serverSelectionTimeoutMS=3000)
 
 
## 第二种
client_one = pymongo.MongoClient(ip,int(port),username=username,password=password,serverSelectionTimeoutMS=3000)
"""
username = "lxb"
password = "lanxiaobai"


class DBMongo(object):

    def __init__(self):
        self.client = MongoClient(f'mongodb://{username}:{password}@121.41.206.74:27017')
        self.db = self.client["python"]['day6-1-1']

    def __del__(self):
        self.client.close()


class DataModel(object):

    def __init__(self, issue_no, number, bonus:str, total:str, lottery_date):
        self.issue_no = issue_no              # 期号
        self.number = number                  # 中奖号码
        self.bonus = bonus.replace(",", "")   # 奖池奖金
        self.total = total.replace(",", "")   # 总投注额
        self.lottery_date = lottery_date      # 开奖日期


class LotterySpider(DBMongo):

    def __init__(self, start=22030, end=22064):
        super(LotterySpider, self).__init__()
        self.start = start
        self.end = end
        self.first = lambda x: x[0] if x else ""

    def get_data(self):
        _url = f"http://datachart.500.com/dlt/history/newinc/history.php?start={self.start}&end={self.end}"

        res = requests.get(_url)

        html = etree.HTML(res.text)
        tdata = html.xpath('//*[@id="tdata"]/tr')
        # print(tdata)
        for item in tdata:
            issue_no = self.first(item.xpath(f'./td[1]/text()'))
            number = ",".join([self.first(item.xpath(f'./td[{i}]/text()')) for i in range(2, 9)])
            bonus = self.first(item.xpath(f'./td[9]/text()'))
            total = self.first(item.xpath(f'./td[14]/text()'))
            lottery_date = self.first(item.xpath(f'./td[15]/text()'))
            print(issue_no, number, bonus, total, lottery_date)

            yield DataModel(issue_no=issue_no, number=number, bonus=bonus, total=total, lottery_date=lottery_date)

    def save_data(self, data: DataModel):
        """ 保存数据到MongoDB """
        self.db.insert_one(data.__dict__)
        print(f"[写入MongoDB数据] {data.__dict__}")

    @classmethod
    def run(cls, start=22030, end=22064):
        """
        运行爬虫
        :param start: 开始期号
        :param end:  结束期号
        """
        spider = cls(start=start, end=end)
        for _data  in spider.get_data():
            spider.save_data(_data)


if __name__ == '__main__':
    LotterySpider.run()





